package cn.melon.mod.dorm;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.common.ModelType;
import cn.melon.helper.BaseRequset;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.LoggerHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.view.admin.dorm.IUpdateDormTable;
import cn.melon.window.MainWindow;

/**
 * 与学籍信息相关的请求类, 在该类中包含了所有与学籍信息相关的请求, 包括学生管理与管理员管理
 * @author corvo
 *
 */
public class DormRequest extends BaseRequset{

    private String TAG = LoggerHelper.makeLogTag(DormRequest.class);
    private Logger dormLogger = LoggerHelper.clientLogger();
    
    private MainWindow m;
    private IUpdateDormTable updateDormTable;
    
    public DormRequest() {
    }
    
    public DormRequest(MainWindow m) {
        this.m = m;
    }
    
    public DormRequest(IUpdateDormTable updateDormTable) {
        this.updateDormTable = updateDormTable;
    }
    
    @Override
    public void makeRequset() {
        //getDormInfo();
        //getAllDormInfo();
        
        DormInfo info = new DormInfo();
        info.setDormNum("梅3A-506");
        info.setNewHonor("好的");
        info.setNewScore("99");
        info.setStudentNum("090143md");
        info.setWaterAndEletricityCharge("45.5");
        //updateDormInfo(info);
        //addDormInfo(info);
    }
    
    /**
     * 添加宿舍信息
     * @Title: addDormInfo
     * @param info void
     */
    public void addDormInfo(DormInfo info) {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ADDINFO");
            jReq.put("content", info.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new RequestHelper(ModelType.DORM)
            .addRequest(jReq.toString())
            .addReceiver("adddorminfo", new IOnRequestListener() {
                
                @Override
                public void onSuccess(String response) {
                    
                    try {
                        JSONObject jRes = new JSONObject(response);
                        dormLogger.debug(response);
                        if (jRes.getInt("code") == 200) {

                        } else {
                            dormLogger.info(TAG + "添加信息失败");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).build().start();
    }
    
    /**
     * 修改宿舍信息(管理员)
     * @Title: updateDormInfo
     * @param info void
     */
    public void updateDormInfo(DormInfo info) {
        
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "UPDATEINFO");
            jReq.put("content", info.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new RequestHelper(ModelType.DORM)
            .addRequest(jReq.toString())
            .addReceiver("updatedorminfo", new IOnRequestListener() {
                
                @Override
                public void onSuccess(String response) {
                    
                    try {
                        JSONObject jRes = new JSONObject(response);
                        dormLogger.debug(response);
                        if (jRes.getInt("code") == 200) {
                            JOptionPane.showMessageDialog(null, "修改宿舍信息成功");
                            getAllDormInfo();
                        } else {
                            JOptionPane.showMessageDialog(null, "修改宿舍信息成功");
                            dormLogger.info(TAG + "修改信息失败");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).build().start();
    }
    
    /**
     * 获取所有人的宿舍信息(管理员)
     * @Title: getAllDormInfo
     */
    public void getAllDormInfo() {
        
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKALL");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new RequestHelper(ModelType.DORM)
            .addRequest(jReq.toString())
            .addReceiver("alldorminfo", new IOnRequestListener() {
                
                @Override
                public void onSuccess(String response) {
                    
                    try {
                        JSONObject jRes = new JSONObject(response);
                        dormLogger.debug(response);
                        if (jRes.getInt("code") == 200) {
                            List<DormInfo> infoList = new ArrayList<>();
                            JSONArray jContent = jRes.getJSONArray("content");
                            for (int i = 0; i < jContent.length(); i++) {
                                DormInfo info = DormInfo.toInfo(jContent.getJSONObject(i));
                                infoList.add(info);
                            }
                            if (updateDormTable != null) {
                                updateDormTable.updateTable(infoList);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).build().start();
    }

    /**
     * 获取当前学生宿舍信息
     * @Title: getDormInfo
     */
    public void getDormInfo() {
        JSONObject jReq = new JSONObject();
        try {
            jReq.put("action", "ASKINFO");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new RequestHelper(ModelType.DORM)
            .addRequest(jReq.toString())
            .addReceiver("dorminfo", new IOnRequestListener() {
                
                @Override
                public void onSuccess(String response) {
                    
                    try {
                        JSONObject jRes = new JSONObject(response);
                        if (jRes.getInt("code") == 200) {
                            JSONObject jContent = jRes.getJSONObject("content");
                            DormInfo dormInfo = DormInfo.toInfo(jContent);
                            if (m != null) {
                                m.updateDormitoryWindow(dormInfo);
                            }

                            dormLogger.debug(TAG + dormInfo.getDormNum());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).build().start();
    }
}
